const net = require('net');
const http = require('http');

// TCP 客户端
function sendTcpMessage(message) {
  const client = new net.Socket();
  client.connect(4220, '192.168.0.109', () => {
    console.log('Connected to TCP Server');
    client.write(message);
  });

  client.on('data', (data) => {
    console.log('Received: ' + data);
    client.destroy(); // kill client after server's response
  });

  client.on('close', () => {
    console.log('Connection closed');
  });
}

// HTTP 服务器
http.createServer((req, res) => {
  let data = '';
  req.on('data', chunk => {
    data += chunk;
  });
  req.on('end', () => {
    sendTcpMessage(data);
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Message sent to TCP server\n');
  });
}).listen(3000, () => {
  console.log('HTTP Server running on port 3000');
});
